<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends CI_Controller {
  public function __construct() {
    parent::__construct();
  }

  public function index() {
    redirect('ajax/getsubscribers');
  }

  // generally this is not required to the outside world
  // TODO: Need to get this inside a model
  public function getsubscribers() {
    if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest") {
      $this->db->select()
        ->from('ci_subscribers sub')
        ->where('sub.status', 1)
        ->order_by('sid', 'ASC');
      $query = $this->db->get();
      $query = $query->result_array();
      print json_encode($query);
    }else{
      header("Location: " . base_url());
    }
  }

  /**
   * This function is actually checking for the valid email
   * and then adding a new email address if it's new to the DB.
   */
  public function addsubscriber() {
    if (!empty($_POST) && is_array($_POST)) {
      $user_email = $this->input->post('email'); // user input
      $this->load->model('subscriber_model', 'subscriber'); // loading the model
      $unique_mail = $this->subscriber->check_for_existing($user_email); // checking for record already present
      if ($unique_mail) {
        $data = array(
          'email' => $user_email,
          'created' => time(),
          'removed' => 0,
          'status' => 1
        );

        $this->db->insert('ci_subscribers', $data);
        echo 'Your email address is added to our list';
      }
      else {
        echo 'Your email address is already in out list';
      }
    }
    else {
      print 'Something went wrong';
    }
  }

  /**
   * This function will send the html for the ajax request
   */
  public function get_twit_block() {
    if (is_ajax_req()) {
      $this->load->view('ajax/ajax_get_twit_block_view');
    }
  }

  public function hom_ajax_more_stories($offset = null) {
    if ($offset != null) {

      $query = $this->db->select('nid')->from('node')->where('status',1)->where('type','article')->get();
      $numer_of_articles = $query->num_rows();

      if ($offset >= $numer_of_articles) {
        echo 'end';
        exit();
      }

//      loading the node model
      $this->load->model('nodes/node_model','node');

      $this->db
        ->select('nid')
        ->from('node')
        ->where('status', 1)
        ->where('type','article')
        ->limit('10',$offset)
        ->order_by('created','DESC');

      $node_list = $this->db->get();

      $node_list = $node_list->result_array();

      $nids = array();
      foreach ($node_list as $node) {
        $nids[] = $node['nid'];
      }

      $nodes = $this->node->node_load_multiple($nids);

      $output = '';
      foreach ($nodes as $node) {
        $node = $node[0];
        $output .= $this->load->view('nodes/nodeteaser',$node,true);
      }

      echo $output;
    }
    else {
      redirect('/');
    }
  }

	public function get_newsletter_subscribers() {
		$server_address = $this->input->server('SERVER_ADDR');
		$host_address =  $this->input->get_request_header('Host');
		if ($server_address == '10.131.25.218' && $host_address = 'amitavroy.com') {
			$this->load->model('admin/mail_model');
			$query_newsletter = $this->db->select()->from('ci_newsletters')->where('status',0)->where('type', 'new post')->get()->row();
			$newsletter_id = $query_newsletter->nid;
			$newsletter_html = $query_newsletter->data;
			$subscribers = $this->mail_model->get_subscribers($newsletter_id);
			$data['subscribers'] = $subscribers;
			$data['mailhtml'] = $newsletter_html;

			echo json_encode($data);
		}
		else {
			show_error('Unauthorized access', 500);
		}
	}

  /**
   * This is an ajax call for
   */
  public function fetch_cache_file_list() {
    if (is_ajax_req()) {
      $this->load->model('admin/cachefile_model', 'cache');
      $data = $this->cache->get_cache_file_listing();
      echo json_encode($data);
    }
  }
}